package com.example.utils.util.LockSupportTest;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * TODO  LockSupport是用来创建locks的基本线程阻塞基元
 *       线程挂起的方法，就是park,对应唤醒就是unpark
 *
 * @ClassName LockSupportTest
 * @Author xiaojiang
 * @Date 2021/9/24 下午 16:44
 */
public class LockSupportTest {

    private static final char[] a = "abcd".toCharArray();
    private static final char[] A = "ABCD".toCharArray();

    private static Thread aThread;
    private static Thread AThread;

    public static void main(String[] args) {
        aThread = new Thread(() -> {
            for (char c : a) {
                System.out.println(c);
                LockSupport.unpark(AThread);
                LockSupport.park();
            }
        });

        AThread = new Thread(() -> {
            for (char c : A) {
                LockSupport.park();
                System.out.println(c);
                LockSupport.unpark(aThread);
            }
        });

        aThread.start();
        AThread.start();
    }

    private ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(50);

    public void sendRelease() {
        scheduledThreadPool.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("-=-=-=-=-=");
            }
        }, 500, TimeUnit.MILLISECONDS);
    }

}
